﻿@namespace MudBlazor.UnitTests.TestComponents
<MudPopoverProvider></MudPopoverProvider>

<MudAutocomplete Label="State" SearchFunc="@SearchStateAsync" 
                 @bind-Value="@StateDetails" Variant="Variant.Outlined" 
                 Margin="MudBlazor.Margin.Dense" ResetValueOnEmptyText="true" Strict="false" ToStringFunc="@(s=> s == null ? null : s.StateName)" />

<MudAutocomplete Label="State" SearchFunc="@SearchStateToStringAsync"
                 @bind-Value="@StateDetails2" Variant="Variant.Outlined"
                 Margin="MudBlazor.Margin.Dense" ResetValueOnEmptyText="true" Strict="false" />

@code {
    public static string __description__ = "Autocomplete should show the dropdown when an item is selected and the string matches ToString or ToStringFunc.";
    public IEnumerable<State> StateList { get; set; }
    public State StateDetails { get; set; }
    public State StateDetails2 { get; set; }

    public async Task<IEnumerable<State>> SearchStateAsync(string value)
    {
        await Task.Delay(5);
        if (string.IsNullOrEmpty(value))
            return states.Select(x => new State(x));
        return states.Where(x => x.Contains(value, StringComparison.InvariantCultureIgnoreCase)).Select(x => new State(x));
    }

    public async Task<IEnumerable<State>> SearchStateToStringAsync(string value)
    {
        await Task.Delay(5);
        if (string.IsNullOrEmpty(value))
            return states.Select(x => new StateToString(x));
        return states.Where(x => x.Contains(value, StringComparison.InvariantCultureIgnoreCase)).Select(x => new StateToString(x));
    }

    public record StateToString : State
    {
        public StateToString(string stateName) : base(stateName) { }

        public override string ToString()
        {
            return StateName;
        }
    }

    public record State
    {
        public string StateName { get; set; }
        public State(string stateName)
        {
            StateName = stateName;
        }
    }

    private string[] states =
    {
        "Alabama", "Alaska", "American Samoa", "Arizona",
        "Arkansas", "California", "Colorado", "Connecticut",
        "Delaware", "District of Columbia", "Federated States of Micronesia",
        "Florida", "Georgia", "Guam", "Hawaii", "Idaho",
        "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky",
        "Louisiana", "Maine", "Marshall Islands", "Maryland",
        "Massachusetts", "Michigan", "Minnesota", "Mississippi",
        "Missouri", "Montana", "Nebraska", "Nevada",
        "New Hampshire", "New Jersey", "New Mexico", "New York",
        "North Carolina", "North Dakota", "Northern Mariana Islands", "Ohio",
        "Oklahoma", "Oregon", "Palau", "Pennsylvania", "Puerto Rico",
        "Rhode Island", "South Carolina", "South Dakota", "Tennessee",
        "Texas", "Utah", "Vermont", "Virgin Island", "Virginia",
        "Washington", "West Virginia", "Wisconsin", "Wyoming",
    };
}
